
var samplesFolder = Folder(app.path + "/Samples/Montage/images/")
// Save the current preferences
var startRulerUnits = app.preferences.rulerUnits
var startTypeUnits = app.preferences.typeUnits
var startDisplayDialogs = app.displayDialogs
// Set Adobe Photoshop CS5 to use pixels and display no dialogs
app.preferences.rulerUnits = Units.PIXELS
app.preferences.typeUnits = TypeUnits.PIXELS
app.displayDialogs = DialogModes.NO

// Create a new document to merge all the samples into





// Use the path to the application and append the samples folder

//Get all the files in the folder
var fileList = samplesFolder.getFiles()


var xSizes=new Array();
var yPoints=new Array();

var lastV=100;
var lastH=100;


var currentColumn=1;
var currentRow=1;

var v=vorig;
var h=horig;

var index=0;

open(fileList[1]);
var docwidth=app.activeDocument.width.value;
var dochei=app.activeDocument.height.value;
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
var mergedDoc = app.documents.add(docwidth, dochei, 72, 'Merged Samples',
NewDocumentMode.RGB, DocumentFill.TRANSPARENT, 1)
// open each file

//Pastes the Background of the montage in
open(fileList[1])
var docName = app.activeDocument.name
app.activeDocument.flatten()
app.activeDocument.selection.selectAll()
app.activeDocument.selection.copy()

lastH=app.activeDocument.width.value;
lastV=app.activeDocument.height.value;
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
var selRegion = Array(
Array(h, v),
Array(h + lastH, v),
Array(h + lastH, v + lastV),
Array(h, v + lastV),
Array(h, v))
app.activeDocument.selection.select(selRegion)
app.activeDocument.paste()


for (var i = 2; i < 2+xMontage*yMontage; i++) {
// The fileList is folders and files so open only files
if (fileList[i] instanceof File) {
open(fileList[i])
// use the document name for the layer name in the merged document
var docName = app.activeDocument.name
// flatten the document so we get everything and then copy
app.activeDocument.flatten()
app.activeDocument.selection.selectAll()
app.activeDocument.selection.copy()

lastH=app.activeDocument.width.value;
lastV=app.activeDocument.height.value;

// don’t save anything we did
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES)
// make a random selection on the document to paste into
// by dividing the document up in 4 quadrants and pasting
// into one of them by selecting that area
var topLeftH = Math.floor(Math.random() * 2)
var topLeftV = Math.floor(Math.random() * 2)
var docH = app.activeDocument.width.value / 2
var docV = app.activeDocument.height.value / 2
var selRegion = Array(
Array(h, v),
Array(h + lastH, v),
Array(h + lastH, v + lastV),
Array(h, v + lastV),
Array(h, v))
app.activeDocument.selection.select(selRegion)
app.activeDocument.paste()
currentColumn=currentColumn+1;
if (currentColumn<=xMontage) h=h+panelWidth+hBorderWidth
if (currentColumn>xMontage) {
    v=v+panelHeight+vBorderWidth;
    h=horig;
    currentColumn=1;
    }
// change the layer name and opacity
app.activeDocument.activeLayer.name = docName
//app.activeDocument.activeLayer.fillOpacity = 50
}
}

// Reset the application preferences
app.preferences.rulerUnits = startRulerUnits
app.preferences.typeUnits = startTypeUnits
app.displayDialogs = startDisplayDialogs

for (var x = 0; x +1< app.activeDocument.layers.length; x++) {
app.activeDocument.layers[x].link(app.activeDocument.layers[x+1]);
}
